package cc.fs.zhihuapp.ui.activity.demo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.soundcloud.android.crop.Crop;

import java.io.File;
import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import cc.fs.zhihuapp.ui.adapter.MyDateAdapter1;
import cc.fs.zhihuapp.utils.FileUtil;
import cc.fs.zhihuapp.utils.ImageUtils;
import cc.fs.zhihuapp.utils.MyCallback;

public class MyAsycHttpActivity extends BaseActivity {

    @InjectView(R.id.head)
    Button head;

    MyDateAdapter1 dateAdapter;
    @InjectView(R.id.resultView)
    ImageView resultView;
    @InjectView(R.id.resultView1)
    ImageView resultView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_asyc_http);
        ButterKnife.inject(this);

        setUpHttp();
    }

    void setUpHttp() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg01);
        L("  width: " + bitmap.getWidth() + " height " + bitmap.getHeight());
        try {
            FileUtil.writeFile(FileUtil.getFile(FileUtil.getCachePath(this, "img1.png")), ImageUtils.bitmap2Bytes(bitmap));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImageUtils.compressBitmap(bitmap,5, new MyCallback.Callback0<Bitmap>() {
            @Override
            public void run(Bitmap param) {
                try {
                    L("  width1: " + param.getWidth() + " height1 " + param.getHeight());
                    FileUtil.writeFile(FileUtil.getFile(FileUtil.getCachePath(_this, "img2.png")), ImageUtils.bitmap2Bytes(param));
                    resultView1.setImageBitmap(param);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Crop.pickImage(_this);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_my_asyc_http, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void handler(Message msg) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
        if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
            beginCrop(result.getData());
        } else if (requestCode == Crop.REQUEST_CROP) {
            handleCrop(resultCode, result);
        }
    }

    private void beginCrop(Uri source) {
        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
        Crop.of(source, destination).asSquare().start(this);
    }

    private void handleCrop(int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            resultView.setImageURI(Crop.getOutput(result));
        } else if (resultCode == Crop.RESULT_ERROR) {
            Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}
